﻿/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://github.com/shenyczz/CSharpKit
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/
using System;
using System.Runtime.InteropServices;

namespace CSharpKit.SVM
{
    /// <summary>
    /// 节点 - 用索引和值封装问题向量中的节点
    /// </summary>
    [Serializable]
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1, Size = 16)]
    public class SvmNode : IComparable<SvmNode>, IEquatable<SvmNode>
    {
        public SvmNode() { }

        public SvmNode(int index, double value)
        {
            Index = index;
            Value = value;
        }

        public int Index { get; set; }
        public double Value { get; set; }


        public int CompareTo(SvmNode other)
        {
            return Index.CompareTo(other.Index);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as SvmNode);
        }

        public bool Equals(SvmNode other)
        {
            return true
                && other != null
                && Index == other.Index
                && Value.Truncate() == other.Value.Truncate()
                ;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode()
            ^ Index.GetHashCode()
            ^ Value.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0}:{1}", Index, Value.Truncate());
        }


        //}}@@@
    }



}
